package com.hjm.project.terminal.manufacturer.controller;

import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import com.hjm.common.utils.security.ShiroUtils;
import com.hjm.project.terminal.manufacturer.domain.Manufacturer;
import com.hjm.project.terminal.manufacturer.service.IManufacturerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.common.utils.poi.ExcelUtil;
import com.hjm.framework.web.page.TableDataInfo;

/**
 * 厂商Controller
 * 
 * @author LOVE
 * @date 2020-11-26
 */
@Controller
@RequestMapping("/terminal/manufacturer")
public class ManufacturerController extends BaseController
{
    private String prefix = "terminal/manufacturer";

    @Autowired
    private IManufacturerService manufacturerService;

    @RequiresPermissions("terminal:manufacturer:view")
    @GetMapping()
    public String manufacturer() {
        return prefix + "/manufacturer";
    }

    /**
     * 查询厂商列表
     */
    @RequiresPermissions("terminal:manufacturer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Manufacturer manufacturer) {
        startPage();
        String manuName = Pattern.compile("\\s*|\t|\r|\n").matcher(manufacturer.getManuName()).replaceAll("");
        manufacturer.setManuName(manuName);
        if (ShiroUtils.isAgent()) {
            manufacturer.setLoginAgentNum(ShiroUtils.getLoginAgentNum());
        }
        List<Manufacturer> list = manufacturerService.selectManufacturerList(manufacturer);
        return getDataTable(list);
    }

    /**
     * 导出厂商列表
     */
    @RequiresPermissions("terminal:manufacturer:export")
    @Log(title = "厂商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Manufacturer manufacturer)
    {
        List<Manufacturer> list = manufacturerService.selectManufacturerList(manufacturer);
        ExcelUtil<Manufacturer> util = new ExcelUtil<Manufacturer>(Manufacturer.class);
        return util.exportExcel(list, "manufacturer");
    }

    /**
     * 新增厂商
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存厂商
     */
    @RequiresPermissions("terminal:manufacturer:add")
    @Log(title = "厂商", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Manufacturer manufacturer) throws Exception {
        Map<String,String> map=manufacturerService.insertManufacturer(manufacturer);
        if ("00".equals(map.get("code"))){
            return AjaxResult.success();
        }else{
            return AjaxResult.warn(map.get("msg"));
        }
    }

    /**
     * 修改厂商
     */
    @GetMapping("/edit/{manuNum}")
    public String edit(@PathVariable("manuNum") Long manuNum, ModelMap mmap)
    {
        Manufacturer manufacturer = manufacturerService.selectManufacturerById(manuNum);
        mmap.put("manufacturer",manufacturer);
        return prefix + "/edit";
    }

    /**
     * 修改保存厂商
     */
    @RequiresPermissions("terminal:manufacturer:edit")
    @Log(title = "厂商", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Manufacturer manufacturer)
    {
        Map<String,String> map=manufacturerService.updateManufacturer(manufacturer);
        if ("00".equals(map.get("code"))){
            return AjaxResult.success();
        }else{
            return AjaxResult.warn(map.get("msg"));
        }
    }

    /**
     * 删除厂商
     */
    @RequiresPermissions("terminal:manufacturer:remove")
    @Log(title = "厂商", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(manufacturerService.deleteManufacturerByIds(ids));
    }

    /**
     * 选择 厂商
     */
    @GetMapping("/selectManufacturer")
    public String selectManufacturer(ModelMap map)
    {
        return prefix + "/selectManufacturer";
    }
}
